﻿
////////////////////////////////////////////////////////////////////////////////////////////
// 文件名: ETLTaskBLL.gen.cs
//
// 模块名称: ETLTaskBLL
//
// 作者: yqf
//
// 历史记录（记录修改记录，修改文件后请添加修改记录，注明修改时间、修改人，修改内容）:
// 2013-01-22    yqf    创建文件
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Model;
using DAL;
using Common;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace BLL
{
    /// <summary>
    /// ETLTask  表业务逻辑层
    /// </summary>
    public partial class ETLTaskBLL
    {
        public ETLTaskBLL(LoginInfo info)
        {
            loginInfo = info;
        }
        public LoginInfo loginInfo
        {
            get;
            set;
        }
        /// <summary>
        /// 根据jobID字段获取ETL_JobInfo实体对象
        /// </summary>
        /// <param name="jobID">jobID</param>
        /// <returns>ETL_JobInfo实体对象</returns>
        public ETL_TaskInfo GetInfoById(string taskID)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetInfoById(taskID);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetInfo", AppError.EROR, 0, ex, "获取ETLTask信息出错。",
                                String.Format("taskID = {0}", taskID));
                return new ETL_TaskInfo();
            }
        }

        public bool CheckName(string name)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).CheckName(name); ;
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "CheckName", AppError.EROR, 0, ex, "CheckName信息出错。", "");
                return false;
            }
        }
        //复制任务
        public bool DoTask(string taskID, string DBType)
        {
            try
            {
                (new ETLTaskDAL(loginInfo)).DoTask(taskID, DBType);
                return true;
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "DoTask", AppError.EROR, 0, ex, "DoTask信息出错。",
                                String.Format("taskID = {0}", taskID));
                return false;
            }
        }
        public bool ClearStep(string taskID)
        {
            try
            {
                (new ETLTaskDAL(loginInfo)).ClearStep(taskID);
                return true;
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "ClearStep", AppError.EROR, 0, ex, "ClearStep信息出错。",
                                String.Format("taskID = {0}", taskID));
                return false;
            }
        }
        /// <summary>
        /// 更新ETL_Job表记录
        /// </summary>
        /// <param name="ETL_JobInfo">ETL_JobInfo实体对象</param>
        public string Update(IDataParameter[] sqlparams)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).Update(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "Update", AppError.EROR, 0, ex, "更新ETLTask信息出错。", "");
                return ex.Message;
            }
        }
        public void UpdateStep(IDataParameter[] sqlparams)
        {
            try
            {
                (new ETLTaskDAL(loginInfo)).UpdateStep(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "Update", AppError.EROR, 0, ex, "更新ETLTask信息出错。", "");
            }
        }
        /// <summary>
        /// 根据jobID字段获取ETL_JobInfo实体对象
        /// </summary>
        /// SqlParameter[] sqlparams</param>
        /// <returns>ETL_JobInfo实体对象列表</returns>
        public List<ETL_TaskInfo> GetList(IDataParameter[] sqlparams = null)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetList(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetInfoList", AppError.EROR, 0, ex, "获取ETLTask信息出错。", "");
                return new List<ETL_TaskInfo>();
            }
        }
        public List<ETL_Task_Details> GetDetails(IDataParameter[] sqlparams = null)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetDetails(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetDetails", AppError.EROR, 0, ex, "获取ETLTask Details信息出错。", "");
                return new List<ETL_Task_Details>();
            }
        }
        public DataTable GetStep()
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetStep();
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetStep", AppError.EROR, 0, ex, "获取ETLTask GetStep信息出错。", "");
                return new DataTable();
            }
        }
        public int GetListCount(IDataParameter[] sqlparams)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetListCount(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetListCount", AppError.EROR, 0, ex, "获取ETLTask实体数量出错。", "");
                return 0;
            }
        }

        public string Delete(string id)
        {
            try
            {
                if ((new ETLTaskDAL(loginInfo)).Delete(id))
                    return "1";
                else
                    return "0";
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "Delete", AppError.EROR, 0, ex, "删除ETLTask信息出错。",
                                String.Format("id = {0}", id));
                return ex.Message;
            }
        }
        public List<ETL_Task_Details_HistoryLog> GetTaskLog(IDataParameter[] sqlparams)
        {
            try
            {
                return (new ETLTaskDAL(loginInfo)).GetTaskLog(sqlparams);
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "GetTaskLog", AppError.EROR, 0, ex, "GetTaskLog信息出错。", "");
                return new List<ETL_Task_Details_HistoryLog>();
            }
        }

        //计划任务
        public void RunTask(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                (new ETLTaskDAL(loginInfo)).RunTask();
            }
            catch (Exception ex)
            {
                Logger.LogError("ETLTaskBLL", "RunTask", AppError.EROR, 0, ex, "RunTask信息出错。", "");
            }
        }

        public DataTable SearchETLTaskLogList(List<SqlParameter> queryParas)
        {
            return (new ETLTaskDAL(loginInfo)).SearchETLTaskLogList(queryParas);
        }

        public int SearchETLTaskLogCount(IDataParameter[] queryParas)
        {
            return (new ETLTaskDAL(loginInfo)).SearchETLTaskLogCount(queryParas);
        }
        public List<ETL_Task_Details_HistoryLog> GetTaskLogItem(string id)
        {
            return (new ETLTaskDAL(loginInfo)).GetTaskLogItem(id);
        }
        public DataTable TaskRunStatus(string taskName, int rows, int page)
        {
            return (new ETLTaskDAL(loginInfo)).TaskRunStatus(taskName, rows, page);
        }
        public DataTable GetTableColumns()
        {
            return (new ETLTaskDAL(loginInfo)).GetTableColumns();
        }

        #region ETL_Task

        public List<ETL_TaskInfo> GetTaskList(string taskName, int taskType, int taskStatus
            , string orderBy, string asc, int pageIndex, int pageSize, out int recordCount)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskList(taskName, taskType, taskStatus
            , orderBy, asc, pageIndex, pageSize, out  recordCount);
            return result;
        }

        public List<ETL_TaskInfo> GetTaskList(string taskName, string taskType, string taskStatus
           , string orderBy, string orderType
           , int pageIndex, int pageSize, out int recordCount)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskList(taskName, taskType, taskStatus
            , orderBy, orderType
            , pageIndex, pageSize, out  recordCount);
            return result;
        }

        public ETL_TaskInfo GetTaskInfoById(string taskID)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskInfoById(taskID);
            return result;
        }

        public List<ETL_TaskInfo> GetTaskByTaskNameWithOutID(string taskID, string taskName)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskByTaskNameWithOutID(taskID, taskName);
            return result;
        }

        public int Insert2(ETL_TaskInfo taskInfo)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.Insert2(taskInfo);
            return result;
        }

        public int Update2(ETL_TaskInfo taskInfo)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.Update2(taskInfo);
            return result;
        }

        public int UpdateStatus(ETL_TaskInfo taskInfo)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.UpdateStatus(taskInfo);
            return result;
        }
        public int Delete2(string taskID, string DBType)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.Delete2(taskID, DBType);
            return result;
        }

        /// <summary>
        /// 保存任务数据
        /// 任务存在则更新任务
        /// 不存在添加任务
        /// 返回：
        ///     0：失败
        ///     1：成功
        ///     2：任务名称相同
        /// </summary>
        /// <param name="taskInfo"></param>
        /// <returns></returns>
        public int SaveTask(ETL_TaskInfo taskInfo)
        {
            var result = 0;
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            //  检查名称是否相同
            var _tmplist = taskDal.GetTaskByTaskNameWithOutID(taskInfo.TaskID, taskInfo.TaskName);
            if (_tmplist.Count > 0)
            {
                return 2;
            }

            var existInfo = taskDal.GetTaskInfoById(taskInfo.TaskID);
            if (existInfo.TaskID == null)// 不存在，直接插入
            {
                result = taskDal.Insert2(taskInfo);

            }
            else// 存在，更新数据库
            {

                result = taskDal.Update2(taskInfo);
            }
            return result;
        }

        public int SaveTaskPlan(ETL_TaskInfo TaskPlanModel)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            return taskDal.SaveTaskPlan(TaskPlanModel);
        }
        #endregion

        #region ETL_Task_Details

        public int SaveTaskDetails(string taskID, List<ETL_Task_Details> taskDetails)
        {
            var result = 0;
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            taskDal.DeleteDetails(taskID);
            if (taskDetails != null && taskDetails.Count > 0)
            {

                foreach (var taskDetailItem in taskDetails)
                {
                    taskDal.InsertDetails(taskDetailItem);
                }
            }
            result = 1;
            return result;
        }

        public List<TaskDetailModel> GetTaskDetailsByTaskID(string taskID, string stepType, int DBType)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskDetailsByTaskID(taskID, stepType, DBType);
            return result;
        }

        #endregion

        #region ETL_Task_Details_HistoryLog

        public List<ETL_Task_Details_HistoryLog> GetTaskLogByTaskID(string taskID, string taskType, string taskStatus, int rows, int page, string DBType)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskLogByTaskID(taskID, taskType, taskStatus, rows, page, DBType);
            return result;
        }

        public List<ETL_Task_Details_HistoryLog> GetAllTaskLog(string taskName, string taskType, string taskStatus, int rows, int page)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetAllTaskLog(taskName, taskType, taskStatus, rows, page);
            return result;
        }
        public int CheckDBJob(string dbName)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.CheckDBJob(dbName);
            return result;
        }
        public DataTable GetTaskHistoryLogByID(string historyID)
        {
            ETLTaskDAL taskDal = new ETLTaskDAL(loginInfo);
            var result = taskDal.GetTaskHistoryLogByID(historyID);
            return result;
        }
        #endregion
    }
}